class_name HoldMagicLightningCloud
extends BaseHoldItem


@onready var shoot_magic_point: Marker2D = $shoot_magic_point
@onready var hit_area: Area2D = $HitArea


func use():
	var player = holder as Player
	player.level.emit_particle("particle_shoot_lightning", shoot_magic_point.global_position)
	AudioManager.play_sound("sfx_lightning_shoot")
	
	var areas = hit_area.get_overlapping_areas()
	if areas.size() <= 0: return
	
	for area in areas:
		var source = area.source 
		shoot_lightning_magic(source)


func shoot_lightning_magic(target_unit: UnitBase):
	await get_tree().create_timer(0.3).timeout
	
	var result = GlobalUtils.raycast_to(target_unit.ray_cast_point.global_position, target_unit.ray_cast_point.global_position + Vector2.DOWN * 1000, 1)
	if result:
		await get_tree().create_timer(0.2).timeout
		
		var magic = (ResourceManager.get_resource("lightning_magic") as PackedScene).instantiate() as LightningMagic
		
		var player = holder as Player
		var config = item_data.item_config as MagicStaffConfig
		magic.attack = config.attack + player.player_data.get_attack()
		magic.critical_rate = config.critical_rate
		magic.critical_bonus = config.critical_bonus
		magic.knockback = config.knockback
		magic.sender = holder
		magic.need_tip = false
		
		holder.level.particles_parent.add_child(magic)
		magic.global_position = result["position"]
		magic.set_is_enemy(false)
		
